home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3433 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  4.9 KB

  1. Path: kbad.eglin.af.mil!rpi!not-for-mail
  2. From: floydb1@lib104.its.rpi.edu (Barry B Floyd)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Converting Numbers to English
  5. Date: 23 Jan 1996 11:32:28 -0500
  6. Organization: Rensselaer Polytechnic Institute, Troy, NY.
  7. Message-ID: <4e32is$6u4@lib104.its.rpi.edu>
  8. References: <4e0pii$grt@mother.usf.edu>
  9. NNTP-Posting-Host: lib104.its.rpi.edu
  10. X-newsreader: xrn 7.04-beta-11
  11.  
  12.  
  13. In article <4e0pii$grt@mother.usf.edu>, oneal@suntan.eng.usf.edu (Aaron Oneal) writes:
  14. |>     Hi all.  I need some kind of algorithm to convert a number like
  15. |> 240,353,740.23 to English (two hundred forty million, three hundred
  16. |> fifty-three thousand, seven hundred forty and twenty-three
  17. |> hundredths).  I don't really need code, although it certainly couldn't
  18. |> hurt.  I just need a point in the right direction.
  19. |> 
  20. |>                       Thanks,
  21. |>             Aaron
  22. |>                 -=>Questor<=-
  23. |> 
  24.  
  25. If using iostreams is ok (i.e. the easiest way I have
  26. found to convert numbers to 'char *'), a String class and
  27. a String 'vector' class is ok, then the following may do.
  28.  
  29. If this is one of your first C/C++ programs, then none of this
  30. would be of value.
  31.  
  32. ( You could also use the 'mod' function to get digits. )
  33.  
  34. Building right to left, first looking for the decimel then looking
  35. for groups of three (i.e. hundreds) digits would seem to make sense.
  36.  
  37. (Pseudo)code to the effect ( assumes us if a String class ):
  38.  
  39. //---------------------------------------------------------------------
  40. // global variables
  41. //---------------------------------------------------------------------
  42.  
  43.     StringVec order_of_mag ( ?? ) ;    // initialize arrays
  44.     order_of_mag[0] = "" ;
  45.     order_of_mag[1] = " ten" ;
  46.     order_of_mag[2] = " hundred" ;
  47.     order_of_mag[3] = " thousand" ; //etc.
  48.  
  49.     StringVec tens ( 10 ) ;
  50.     tens[0] = "" ;
  51.     tens[1] = "" ;
  52.     tens[2] = " twenty" ;
  53.     tens[3] = " thirty" ;         //etc.
  54.  
  55.     StringVec ones ( 10 ) ;
  56.     ones[0] = "" ;
  57.     ones[1] = " one" ;
  58.     ones[2] = " two" ;        //etc. thru nineteen
  59.  
  60. //---------------------------------------------------------------------
  61. String     SetoThree ( String str_number ) 
  62. //---------------------------------------------------------------------
  63. {
  64.     String result = String_EOS ;
  65.  
  66.     if ( str_number.length () == 0 )
  67.         return ( result ) ;
  68.  
  69.     if ( str_number.length () == 3 )
  70.     {
  71.         result += ones[atoi ( (char *)str_number[1]] ;
  72.         result += order_of_mag[2] ;
  73.     }
  74.  
  75.     if ( str_number.length () > 1 )
  76.     {
  77.         if ( atoi ( (char *)str_number[2] ) > 1 )
  78.             result += tens[atoi ( (char*)str_number[2]];
  79.             result += ones[atoi ( (char*)str_number[3]];
  80.         else
  81.             result += ones[atoi ( (char*)str_number.at(2,2)];
  82.     }
  83.     else
  84.         result += ones[ atoi ( (char*)str_number[3]];
  85.  
  86.     return ( result ) ;
  87. }
  88.  
  89. //---------------------------------------------------------------------
  90. String     Convert ( String str_num ) 
  91. //---------------------------------------------------------------------
  92. {
  93.     int index = 0 ;
  94.  
  95.     int num_length = str_num.length () ;
  96.  
  97.     int sets_of_three = num_length / 3 ;
  98.  
  99.     int rest = num_length mod 3 ;
  100.  
  101.     if ( rest > 0 )
  102.     {
  103.         String result_str += SetoThree (str_num.at ( 0, rest ) ;
  104.                result_str += order_of_mag[sets_of_three+1] ;
  105.     }
  106.  
  107.     for ( index = 0 ; index < sets_of_three - 1 ; index++ )
  108.     {
  109.         result_str += SetoThree (str_num.at ((3*index)+rest, 3) ;
  110.         result_str += order_of_mag[sets_of_three-index+1] ;
  111.     }
  112.  
  113.     if ( sets_of_three > 0 )
  114.         result_str += SetoThree (str_num.at ( num_length-3, 3 ) ;
  115.  
  116.     
  117.  
  118. }
  119.  
  120. //---------------------------------------------------------------------
  121. //---------------------------------------------------------------------
  122. main ()
  123. //---------------------------------------------------------------------
  124. {
  125.    while ( TRUE )             // type CTRL-C to end program
  126.    {
  127.     cout     << "Enter your number: " ;    
  128.  
  129.       { // scope for variables and objects 
  130.  
  131.     float your_number ;
  132.  
  133.     cin     >> your_number ;    // get a number
  134.  
  135.     ostrstream strstr_number    // convert to String from float
  136.     strstr_number << your_number ;
  137.     String str_number = strstr_number.str () ;
  138.     strstr_number.freeze ( 0 ) ;
  139.  
  140.     int dec_place = str_number.index ( "." ) ;
  141.  
  142.     String english ;
  143.  
  144.     if ( dec_place != -1 )        // no decimel
  145.     {
  146.         english  = Convert ( str_number.at ( 0, dec_place ) ) ;
  147.         english += "and" ;
  148.         english += Convert ( str_number.from ( dec_place+1 ) ) ;
  149.         english += order_of_mag[str_number.length()-dec_place-1];
  150.          english += "ths" ;
  151.     }
  152.     else
  153.         english  = Convert ( str_number ) ;
  154.  
  155.         
  156.     cout     << "Your number in english is: " << endl 
  157.         << english << endl ;
  158.  
  159.       }    // end scope
  160.    } // end loop
  161. } // end main
  162.  
  163. //---------------------------------------------------------------------
  164. //---------------------------------------------------------------------
  165. //---------------------------------------------------------------------
  166. -- 
  167. +--------------------------------------------------------------------+ 
  168. | Barry B. Floyd                   \\\               floydb1@rpi.edu |
  169. | RPI Alum. '84 '87 '88              \\\                             |
  170. +--------------------------------------------------------------------+
  171.